//% color="#348aed" iconWidth=50 iconHeight=40
namespace UNIHIKER_BOX{

    //% block="传感器" blockType="tag"
    export function tagtest1() {}


    
    //% block="Read angle sensor" blockType="reporter"  
    export function ANGLEvalue(parameter: any, block: any) { 

        Generator.addImport('from pinpong.board import Board,Pin');
        Generator.addImport('from pinpong.extension.unihiker import *');
        Generator.addInit(`board_begin`,`Board().begin()`);
        Generator.addInit(`angle_sensor`,`angle = Pin(Pin.P10, Pin.ANALOG)`);
        Generator.addCode(`angle.read_analog()`)
   
    }



    //% block="Read joystick [AXIS]" blockType="reporter"  
    //% AXIS.shadow="dropdown" AXIS.options="AXIS"
    export function JOYSTICKvalue(parameter: any, block: any) { 
        let axis=parameter.AXIS.code;

        Generator.addImport('from pinpong.board import Board,Pin');
        Generator.addImport('from pinpong.extension.unihiker import *');
        Generator.addInit(`board_begin`,`Board().begin()`);
        if (axis == "x"){
            Generator.addInit(`x_axis`,`x_axis = Pin(Pin.P0, Pin.ANALOG)`);
            Generator.addCode(`x_axis.read_analog()`);
        }
        else if(axis == "y"){
            Generator.addInit(`y_axis`,`y_axis = Pin(Pin.P1, Pin.ANALOG)`);
            Generator.addCode(`y_axis.read_analog()`);
        }

    }



    //% block="Read temperature and humidity sensor [DATA]" blockType="reporter"  
    //% DATA.shadow="dropdown" DATA.options="DATA"
    export function Humiture(parameter: any, block: any) { 
        let data=parameter.DATA.code;

        Generator.addImport('from pinpong.board import Board,Pin');
        Generator.addImport('from pinpong.libs.dfrobot_aht20 import AHT20');
        Generator.addInit(`board_begin`,`Board().begin()`);
        Generator.addInit(`aht`,`aht = AHT20()`);
        Generator.addCode(`aht.${data}`)
   
    }


    //% block="---"
    export function noteSep6() {}


    //% block="The [DIRECTION] button is pressed ?" blockType="boolean"  
    //% DIRECTION.shadow="dropdown" DIRECTION.options="DIRECTION"
    export function READbutton(parameter: any, block: any) { 
        let direction=parameter.DIRECTION.code;

        Generator.addImport('from pinpong.board import Board,Pin');
        Generator.addImport('from pinpong.extension.unihiker import *');
        Generator.addInit(`board_begin`,`Board().begin()`);
        if (direction == "up"){
            Generator.addInit(`up_button`,`up_button = Pin(Pin.P27, Pin.IN)`);
            Generator.addCode(`(up_button.read_digital() == 0)`)
        }
        else if (direction == "down"){
            Generator.addInit(`down_button`,`down_button = Pin(Pin.P28, Pin.IN)`);
            Generator.addCode(`(down_button.read_digital() == 0)`)
        }
        else if (direction == "left"){
            Generator.addInit(`left_button`,`left_button = Pin(Pin.P11, Pin.IN)`);
            Generator.addCode(`(left_button.read_digital() == 0)`)
        }
        else{
            Generator.addInit(`right_button`,`right_button = Pin(Pin.P12, Pin.IN)`);
            Generator.addCode(`(right_button.read_digital() == 0)`)
        }
   
    }

    //% block="The conductance is turned on ?" blockType="boolean"  
    export function Conductance(parameter: any, block: any) { 

        Generator.addImport('from pinpong.board import Board,Pin');
        Generator.addImport('from pinpong.extension.unihiker import *');
        Generator.addInit(`board_begin`,`Board().begin()`);
        Generator.addInit(`conductance`,`conductance = Pin(Pin.P7, Pin.IN)`);
        Generator.addCode(`(conductance.read_digital() == True)`)
   
    }



    //% block="Infrared sensor detects object ?" blockType="boolean"  
    export function READinfrared(parameter: any, block: any) { 

        Generator.addImport('from pinpong.board import Board,Pin');
        Generator.addImport('from pinpong.extension.unihiker import *');
        Generator.addInit(`board_begin`,`Board().begin()`);
        Generator.addInit(`infrared`,`infrared = Pin(Pin.P13, Pin.IN)`);
        Generator.addCode(`(infrared.read_digital() == 0)`)
    
    }


    //% block="---"
    export function noteSep7() {}


    //% block="Initialize RFID module [VER]" blockType="command"
    //% VER.shadow="dropdown" VER.options="VER"
    export function RFID_init(parameter: any, block: any) { 
        let ver=parameter.VER.code;

        if (ver == "V1"){

            Generator.addImport('from pinpong.board import Board,Pin');
            Generator.addImport('from dfrobot_pn7150 import *');
            Generator.addInit(`board_begin`,`Board().begin()`);
            Generator.addInit(`RFID_object`,`RFID = DFRobot_PN7150(i2c_addr=PN7150_I2C_ADDR, bus_num=4)`);
            Generator.addInit(`RFID_connect`,`
while (RFID.connect() == False):
    print("init fail!")
    time.sleep(1)
print("successful connection.")
`);
            Generator.addInit(`RFID_mode`,`RFID.mode_rw()`);

        }
        else {

            Generator.addImport('from pinpong.board import Board,Pin');
            Generator.addImport('from dfrobot_fm17550 import Rfid');
            Generator.addImport('import time');
            Generator.addInit(`board_begin`,`Board().begin()`);
            Generator.addInit(`RFID_object`,`RFID = Rfid()`);
            Generator.addInit(`RFID_connect`,`
while (RFID.begin() == False):
    print("init fail!")
    time.sleep(1)
print("successful connection.")
`); 

        }

    }


    //% block="Write [DATA] to byte [BYTE_NUM] of RFID block [BLOCK_NUM]" blockType="command" 
    //% DATA.shadow="range" DATA.params.min=0 DATA.params.max=255 DATA.defl=200
    //% BYTE_NUM.shadow="dropdown" BYTE_NUM.options="BYTE_NUM"
    //% BLOCK_NUM.shadow="dropdown" BLOCK_NUM.options="BLOCK_NUM"
    export function RFID_write(parameter: any, block: any) { 
        let data=parameter.DATA.code;
        let block_num=parameter.BLOCK_NUM.code;
        let byte_num=parameter.BYTE_NUM.code;

        Generator.addCode(`RFID.write_index_data(${block_num}, ${byte_num}, ${data})`);
    }



    //% block="Read all data from block [BLOCK_NUM] of the RFID module(string)" blockType="reporter" 
    //% BLOCK_NUM.shadow="dropdown" BLOCK_NUM.options="BLOCK_NUM"
    export function RFID_read_all(parameter: any, block: any) { 
        let block_num=parameter.BLOCK_NUM.code;

        Generator.addCode(`RFID.read_data(${block_num})`);
    }



    //% block="Read byte [BYTE_NUM] of RFID block [BLOCK_NUM]" blockType="reporter" 
    //% BLOCK_NUM.shadow="dropdown" BLOCK_NUM.options="BLOCK_NUM"
    //% BYTE_NUM.shadow="dropdown" BYTE_NUM.options="BYTE_NUM"
    export function RFID_read(parameter: any, block: any) { 
        let block_num=parameter.BLOCK_NUM.code;
        let byte_num=parameter.BYTE_NUM.code;

        Generator.addCode(`RFID.read_data(${block_num}, ${byte_num})`);
    }



    //% block="Read the UID of the tag card" blockType="reporter" 
    export function RFID_read_UID(parameter: any, block: any) { 

        Generator.addCode(`RFID.read_uid()`);
    }


    //% block="Detected card. UID:[UID]?" blockType="boolean" 
    //% UID.shadow="string" UID.defl="acee50e3"
    export function RFID_SCAN_UID(parameter: any, block: any) { 
        
        let uid=parameter.UID.code;
        Generator.addCode(`RFID.scan(${uid})`);
    }


    //% block="Detected card?" blockType="boolean" 
    export function RFID_SCAN(parameter: any, block: any) { 

        Generator.addCode(`RFID.scan()`);
    }



    //% block="---"
    export function noteSep1() {}

    //% block="执行器" blockType="tag"
    export function tagtest2() {}



    //% block="Set [COLOR1] LED [LEVEL]" blockType="command"  
    //% COLOR1.shadow="dropdown" COLOR1.options="COLOR1"
    //% LEVEL.shadow="dropdown" LEVEL.options="LEVEL"
    export function LEDdigital(parameter: any, block: any) { 
        let color=parameter.COLOR1.code;
        let level=parameter.LEVEL.code;

        Generator.addImport('from pinpong.board import Board,Pin');
        Generator.addImport('from pinpong.extension.unihiker import *');
        Generator.addInit(`board_begin`,`Board().begin()`);

        if (color == "red"){
            Generator.addInit(`red_led`,`red_led = Pin(Pin.P2, Pin.PWM)`);
            Generator.addCode(`red_led.write_analog(${level})`);
        }
        else if (color == "yellow"){
            Generator.addInit(`yellow_led`,`yellow_led = Pin(Pin.P3, Pin.PWM)`);
            Generator.addCode(`yellow_led.write_analog(${level})`);
        }
        else{
            if (level == 1023){
                Generator.addInit(`green_led`,`green_led = Pin(Pin.P4, Pin.OUT)`);
                Generator.addCode(`green_led.write_digital(1)`);
            }
            else if(level == 0){
                Generator.addInit(`green_led`,`green_led = Pin(Pin.P4, Pin.OUT)`);
                Generator.addCode(`green_led.write_digital(0)`);
            }
        }
   
    }

    //% block="Set [COLOR2] LED brightness[BRIGHT]" blockType="command"  
    //% COLOR2.shadow="dropdown" COLOR2.options="COLOR2"
    //% BRIGHT.shadow="range" BRIGHT.params.min=0 BRIGHT.params.max=1023 BRIGHT.defl=512
    export function LEDanalog(parameter: any, block: any) { 
        let color=parameter.COLOR2.code;
        let bright=parameter.BRIGHT.code;

        Generator.addImport('from pinpong.board import Board,Pin');
        Generator.addImport('from pinpong.extension.unihiker import *');
        Generator.addInit(`board_begin`,`Board().begin()`);
        if (color == "red"){
            Generator.addInit(`red_led`,`red_led = Pin(Pin.P2, Pin.PWM)`);
            Generator.addCode(`red_led.write_analog(${bright})`);
        }
        else{
            Generator.addInit(`yellow_led`,`yellow_led = Pin(Pin.P3, Pin.PWM)`);
            Generator.addCode(`yellow_led.write_analog(${bright})`);
        }
   
    }


    //% block="---"
    export function noteSep8() {

    } 


    //% block="Set water pump [LEVEL]" blockType="command"  
    //% LEVEL.shadow="dropdown" LEVEL.options="LEVEL1"
    export function waterpump_digital(parameter: any, block: any) { 
        let level=parameter.LEVEL.code;

        Generator.addImport('from pinpong.board import Board,Pin');
        Generator.addImport('from pinpong.extension.unihiker import *');
        Generator.addInit(`board_begin`,`Board().begin()`);

        Generator.addInit(`water_pump`,`water_pump = Pin(Pin.P25, Pin.OUT)`);
        Generator.addCode(`water_pump.write_digital(${level})`);
   
    }


    //% block="---"
    export function noteSep2() {

    }

    //% block="Initializes the RGB light" blockType="command"  
    export function INITRGB(parameter: any, block: any) { 

        Generator.addImport('from pinpong.board import Board,Pin');
        Generator.addImport('from pinpong.board import NeoPixel');
        Generator.addImport('from pinpong.extension.unihiker import *');
        Generator.addInit(`board_begin`,`Board().begin()`);
        Generator.addInit(`init_RGB`,`RGB = NeoPixel(Pin((Pin.P9)),4)`);
        Generator.addCode(`RGB.brightness(128)`)
   
    }

    //% block="RGB lights Set [BRIGHT]" blockType="command"  
    //% BRIGHT.shadow="range" BRIGHT.params.min=0 BRIGHT.params.max=255 BRIGHT.defl=128
    export function RGBbrightness(parameter: any, block: any) { 
        let bright=parameter.BRIGHT.code;

        Generator.addCode(`RGB.brightness(${bright})`)
   
    }

    //% block="All RGB lights are off" blockType="command"  
    export function RGBoff(parameter: any, block: any) { 

        Generator.addCode(`RGB.clear()`)
   
    }

    //% block="Set the [COLOR] of the [NUMBER] light" blockType="command"  
    //% NUMBER.shadow="dropdown" NUMBER.options="NUMBER"
    //% COLOR.shadow="colorPalette"
    export function RGBcolor1(parameter: any, block: any) { 
        let number=parameter.NUMBER.code;
        let color=parameter.COLOR.code; //获取传进来的值

        var r = 0;
        var g = 0;
        var b = 0;
        try {
            if ( color.length == 8 ) {//分别截取RGB值然后转换为数字值
                r = parseInt(color.substring(2, 4), 16);
                g = parseInt(color.substring(4, 6), 16);
                b = parseInt(color.substring(6, 8), 16);
            }
        } catch(e) {
            return '';
        }

        Generator.addCode(`RGB[${number}] = (${r},${g},${b})`)
   
    }

    //% block="RGB lights [NUMBER1] to [NUMBER2] display [COLOR]" blockType="command"  
    //% NUMBER1.shadow="dropdown" NUMBER1.options="NUMBER"
    //% NUMBER2.shadow="dropdown" NUMBER2.options="NUMBER"
    //% COLOR.shadow="colorPalette"
    export function RGBcolor2(parameter: any, block: any) { 
        let number1=parameter.NUMBER1.code;
        let number2=parameter.NUMBER2.code;
        let color=parameter.COLOR.code; 

        Generator.addCode(`RGB.range_color(${number1},${number2},${color})`)
   
    }



    //% block="RGB lights [NUMBER1] to [NUMBER2] display gradient color. tone from [RANGE1] to [RANGE2]" blockType="command"  
    //% NUMBER1.shadow="dropdown" NUMBER1.options="NUMBER"
    //% NUMBER2.shadow="dropdown" NUMBER2.options="NUMBER"
    //% RANGE1.shadow="range" RANGE1.params.min=0 RANGE1.params.max=360 RANGE1.defl=1
    //% RANGE2.shadow="range" RANGE2.params.min=0 RANGE2.params.max=360 RANGE2.defl=360
    export function RGBtone(parameter: any, block: any) { 
        let number1=parameter.NUMBER1.code;
        let number2=parameter.NUMBER2.code;
        let range1=parameter.RANGE1.code; 
        let range2=parameter.RANGE2.code; 

        Generator.addCode(`RGB.rainbow(${number1},${number2},${range1},${range2})`)
   
    }

    //% block="RGB light moves [NUM] unit" blockType="command"  
    //% NUM.shadow="number" NUM1.defl="0"
    export function RGBmove1(parameter: any, block: any) { 
        let num=parameter.NUM.code;

        Generator.addCode(`RGB.shift(${num})`)
   
    }

    //% block="The RGB light rotates [NUM] unit" blockType="command"  
    //% NUM.shadow="number" NUM.defl="0"
    export function RGBmove2(parameter: any, block: any) { 
        let num=parameter.NUM.code;

        Generator.addCode(`RGB.rotate(${num})`)
   
    }




    //% block="RED[RED] GREEN[GREEN] BLUE[BLUE]" blockType="reporter"  
    //% RED.shadow="range" RED.params.min=0 RED.params.max=255 RED.defl=255
    //% GREEN.shadow="range" GREEN.params.min=0 GREEN.params.max=255 GREEN.defl=255
    //% BLUE.shadow="range" BLUE.params.min=0 BLUE.params.max=255 BLUE.defl=255
    export function color(parameter: any, block: any) { 
        let red=parameter.RED.code;
        let green=parameter.GREEN.code;
        let blue=parameter.BLUE.code;

        Generator.addCode(`(${red},${green},${blue})`)
   
    }


    //% block="---"
    export function noteSep3() {}

    //% block="高配功能" blockType="tag"
    export function tagtest3() {}

    //% block="Read the ultrasonic sensor distance" blockType="reporter"  
    export function SR04ultrasonic(parameter: any, block: any) { 

        Generator.addImport('from pinpong.board import Board,Pin');
        Generator.addImport('from pinpong.extension.unihiker import *');
        Generator.addImport('from pinpong.board import SR04_URM10');
        Generator.addInit(`board_begin`,`Board().begin()`);
        Generator.addInit(`ultrasonic`,`ultrasonic = SR04_URM10(Pin((Pin.P14)),Pin((Pin.P15)))`);
        Generator.addCode(`ultrasonic.distance_cm()`)
   
    }



    //% block="---"
    export function noteSep4() {}
    
    //% block="Initialize  line tracker at I2C address 0x7C" blockType="command"  
    export function Tracker_init(parameter: any, block: any) { 
        Generator.addImport('from pinpong.board import Board,Pin');
        Generator.addImport('from pinpong.libs.dfrobot_box_line_sensor import *');
        Generator.addInit(`board_begin`,`Board().begin()`);
        Generator.addInit(`Tracker_object`,`LS = Box_LineSensor(0x30)`);
   
    }


    //% block="Calibrate line tracker sensor [PROBE] threshold to [THRESHOLD_SET]" blockType="command" 
    //% PROBE.shadow="dropdown" PROBE.options="PROBE"
    //% THRESHOLD_SET.shadow="range" THRESHOLD_SET.params.min=0 THRESHOLD_SET.params.max=255 THRESHOLD_SET.defl=150
    export function THRESHOLD_SET(parameter: any, block: any) { 
        let probe=parameter.PROBE.code;
        let threshold_set=parameter.THRESHOLD_SET.code;

        Generator.addCode(`LS.set_threshod_number(${probe}, ${threshold_set})`);

    }


    //% block="Get all line sensor thresholds(array)" blockType="reporter" 
    export function THRESHOLD_GET(parameter: any, block: any) { 

        Generator.addCode(`LS.get_threshod_value()`);

    }


    //% block="Read all probe analog values(array)" blockType="reporter" 
    export function Tracker_read_all_analog(parameter: any, block: any) { 

        Generator.addCode(`LS.get_adc_value()`);

    }


    //% block="Read all probe digital values(string)" blockType="reporter" 
    export function Tracker_read_all_digital(parameter: any, block: any) { 

        Generator.addCode(`LS.get_adc_all()`);

    }


    //% block="Read all digital probe values equal [VALUE]?" blockType="boolean" 
    //% VALUE.shadow="string" VALUE.defl="00000"
    export function Tracker_read_all_boolean(parameter: any, block: any) { 
        
        let value=parameter.VALUE.code;
        Generator.addCode(`LS.get_adc_all() == ${value}`);
    }


    //% block="Read digital value of line sensor [PROBE]" blockType="boolean"
    //% PROBE.shadow="dropdown" PROBE.options="PROBE"
    export function Tracker_read_digital(parameter: any, block: any) { 
        
        let probe=parameter.PROBE.code;
        Generator.addCode(`LS.get_adc(${probe})`);
    }



    //% block="---"
    export function noteSep5() {}

    //% block="Motor [MOTOR] [TURN] at [SPEED]" blockType="command"  
    //% MOTOR.shadow="dropdown" MOTOR.options="MOTOR" 
    //% TURN.shadow="dropdown" TURN.options="TURN" 
    //% SPEED.shadow="range" SPEED.params.min=0 SPEED.params.max=1023 SPEED.defl=800
    export function MOTORrotation(parameter: any, block: any) { 
        let motor=parameter.MOTOR.code;
        let turn=parameter.TURN.code;
        let speed=parameter.SPEED.code;


        Generator.addImport('from pinpong.board import Board,Pin');
        Generator.addImport('from pinpong.extension.unihiker import *');
        Generator.addInit(`board_begin`,`Board().begin()`);

        if (motor == "M1"){
            Generator.addInit(`M1_veer`,`M1_veer = Pin(Pin.P5, Pin.OUT)`);
            Generator.addInit(`M1_speed`,`M1_speed = Pin(Pin.P8, Pin.PWM)`);
            Generator.addCode(`M1_veer.write_digital(${turn})`);
            Generator.addCode(`M1_speed.write_analog(${speed})`);
        }
        else if (motor == "M2"){
            Generator.addInit(`M2_veer`,`M2_veer = Pin(Pin.P6, Pin.OUT)`);
            Generator.addInit(`M2_speed`,`M2_speed = Pin(Pin.P16, Pin.PWM)`);
            Generator.addCode(`M2_veer.write_digital(${turn})`);
            Generator.addCode(`M2_speed.write_analog(${speed})`);
        }
        else{
            Generator.addInit(`M1_veer`,`M1_veer = Pin(Pin.P5, Pin.OUT)`);
            Generator.addInit(`M1_speed`,`M1_speed = Pin(Pin.P8, Pin.PWM)`);
            Generator.addInit(`M2_veer`,`M2_veer = Pin(Pin.P6, Pin.OUT)`);
            Generator.addInit(`M2_speed`,`M2_speed = Pin(Pin.P16, Pin.PWM)`);
            Generator.addCode(`M1_veer.write_digital(${turn})`);
            Generator.addCode(`M1_speed.write_analog(${speed})`);
            Generator.addCode(`M2_veer.write_digital(${turn})`);
            Generator.addCode(`M2_speed.write_analog(${speed})`);
        }
        
    }

    //% block="Motor [MOTOR] stop" blockType="command"  
    //% MOTOR.shadow="dropdown" MOTOR.options="MOTOR" 
    export function MOTORstop(parameter: any, block: any) { 
        let motor=parameter.MOTOR.code;

        Generator.addImport('from pinpong.board import Board,Pin');
        Generator.addImport('from pinpong.extension.unihiker import *');
        Generator.addInit(`board_begin`,`Board().begin()`);

        if (motor == "M1"){
            Generator.addInit(`M1_speed`,`M1_speed = Pin(Pin.P8, Pin.PWM)`);
            Generator.addCode(`M1_speed.write_analog(0)`);
        }
        else if(motor == "M2"){
            Generator.addInit(`M2_speed`,`M2_speed = Pin(Pin.P16, Pin.PWM)`);
            Generator.addCode(`M2_speed.write_analog(0)`);
        }
        else{
            Generator.addInit(`M1_speed`,`M1_speed = Pin(Pin.P8, Pin.PWM)`);
            Generator.addInit(`M2_speed`,`M2_speed = Pin(Pin.P16, Pin.PWM)`);
            Generator.addCode(`M1_speed.write_analog(0)`);
            Generator.addCode(`M2_speed.write_analog(0)`);
        }
   
    }


}

